﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Controllers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Areas.Declare.Controllers
{
    public class DeclareInfoController : BaseController
    {
        public IZS_DeclareInfoService ZS_DeclareInfoService { get; set; }
        public IZS_LogService ZS_LogService { get; set; }
        // GET: Declare/DeclareInfo
        public string Index(PaginationQuery paginationQuery)
        {
            int total = 0;
            //var declareList = ZS_DeclareInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
            //    s => s.CreateTime, true).AsQueryable();
            var declareList = ZS_DeclareInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, " DelFlag=0 ", "");
            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(declareList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }
        #region 获取编辑ViewModel
        /// <summary>
        /// 获取编辑数据
        /// </summary>
        /// <returns></returns>
        public string GetEditModel(int Id)
        {
            string res = string.Empty;
            JsonMessage msg = new JsonMessage();
            ZS_DeclareInfo declareModel = ZS_DeclareInfoService.GetModel(Id);
            if (declareModel != null)
            {
                object obj = ZS_DeclareInfoService.GetEditModel(declareModel);
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(obj));
                res = JsonHandler.ToJson(msg);
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "获取数据错误!");
                res = JsonHandler.ToJson(msg);
            }
            return res;
        } 
        #endregion
        #region 添加
        #region old
        //public string Create(ZS_DeclareInfo declareInfo)
        //{
        //    declareInfo.DelFlag = delflagNormal;
        //    declareInfo.CreateTime = DateTime.Now;
        //    declareInfo.CreateOperator = LoginUser.UName;
        //    declareInfo.CreateOperatorId = LoginUser.Id;

        //    ZS_DeclareInfoService.Add(declareInfo);
        //    if (declareInfo.Id > 0)
        //    {
        //        ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, declareInfo.Id, "添加申报表信息", LoginUser.UName, LoginUser.Id);
        //    }
        //    JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
        //    return JsonHandler.ToJson(message);
        //}
        //public string Create(ViewDeclare declareView)
        //{
        //    declareView.DelFlag = delflagNormal;
        //    declareView.CreateTime = DateTime.Now;
        //    declareView.CreateOperator = LoginUser.UName;
        //    declareView.CreateOperatorId = LoginUser.Id;
        //    ZS_DeclareInfoService.Add(declareView);

        //    if (declareView.Id > 0)
        //    {
        //        // 添加共同申报人数据  更新关联的房号信息表的 DeclareId 字段
        //        if (!string.IsNullOrEmpty(declareView.MemberIds))
        //        {
        //            string[] memberIds = declareView.MemberIds.Split(',');
        //            ZS_DeclareInfoService.AddDeclareJointApplicant(declareView.Id, memberIds);
        //        }
        //        if (!string.IsNullOrEmpty(declareView.HouseIds))
        //        {
        //            string[] houseIds = declareView.HouseIds.Split(',');
        //            ZS_DeclareInfoService.UpdateDeclareHouse(declareView.Id, houseIds);
        //        }

        //        ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, declareView.Id, "添加申报表信息", LoginUser.UName, LoginUser.Id);
        //    }
        //    JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
        //    return JsonHandler.ToJson(message);
        //} 
        #endregion
        public string Create(string viewStr)
        {
            try
            {
                ViewDeclare declareView = JsonHandler.Deserialize<ViewDeclare>(viewStr);

                ZS_DeclareInfo declareModel = new ZS_DeclareInfo();
                declareModel.FamilyId = declareView.FamilyId;
                declareModel.MemberId = declareView.MemberId;
                declareModel.MemberName = declareView.MemberName;
                declareModel.BuildingId = declareView.BuildingId;
                declareModel.DelFlag = delflagNormal;
                declareModel.IsTransferCheck = (int)AllEnum.IsTransferCheckEnum.默认;
                declareModel.CreateTime = DateTime.Now;
                declareModel.CreateOperator = LoginUser.UName;
                declareModel.CreateOperatorId = LoginUser.Id;
                ZS_DeclareInfoService.Add(declareModel);

                if (declareModel.Id > 0)
                {
                    // 添加共同申报人数据  更新关联的房号信息表的 DeclareId 字段
                    if (!string.IsNullOrEmpty(declareView.MemberIds))
                    {
                        string[] memberIds = declareView.MemberIds.Split(',');
                        ZS_DeclareInfoService.AddDeclareJointApplicant(declareModel.Id, memberIds);
                    }
                    if (declareView.HouseList.Count > 0)
                    {
                        ZS_DeclareInfoService.AddDeclareHouse(declareModel.Id, declareView.BuildingId ?? 0, declareView.HouseList);
                    }

                    ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, declareModel.Id, "添加申报表信息", LoginUser.UName, LoginUser.Id);
                }
                JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
                return JsonHandler.ToJson(message);
            }
            catch(Exception ex)
            {
                JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "添加失败!", ex.Message);
                return JsonHandler.ToJson(message);
            }
        }


        #endregion
        #region 编辑
        #region old
        //public string Edit(ZS_DeclareInfo declareInfo)
        //{
        //    ZS_DeclareInfo editDeclare = ZS_DeclareInfoService.GetModel(declareInfo.Id);
        //    string res = string.Empty;
        //    if (editDeclare == null)
        //    {
        //        res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
        //    }
        //    else
        //    {
        //        editDeclare.BuildingId = declareInfo.BuildingId;
        //        editDeclare.FamilyId = declareInfo.FamilyId;
        //        editDeclare.MemberId = declareInfo.MemberId;
        //        editDeclare.MemberName = declareInfo.MemberName;

        //        bool success = ZS_DeclareInfoService.Update(editDeclare);
        //        if (success)
        //        {
        //            ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, declareInfo.Id, "编辑申报表信息", LoginUser.UName, LoginUser.Id);
        //            res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
        //        }
        //    }

        //    return res;
        //}
        //public string Edit(ViewDeclare declareView)
        //{
        //    ZS_DeclareInfo editDeclare = ZS_DeclareInfoService.GetModel(declareView.Id);
        //    string res = string.Empty;
        //    if (editDeclare == null)
        //    {
        //        res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
        //    }
        //    else
        //    {
        //        editDeclare.BuildingId = declareView.BuildingId;
        //        editDeclare.FamilyId = declareView.FamilyId;
        //        editDeclare.MemberId = declareView.MemberId;
        //        editDeclare.MemberName = declareView.MemberName;

        //        bool success = ZS_DeclareInfoService.Update(editDeclare);
        //        if (success)
        //        {
        //            if (!string.IsNullOrEmpty(declareView.MemberIds))
        //            {
        //                string[] memberIds = declareView.MemberIds.Split(',');
        //                ZS_DeclareInfoService.AddDeclareJointApplicant(declareView.Id, memberIds);
        //            }
        //            if (!string.IsNullOrEmpty(declareView.HouseIds))
        //            {
        //                string[] houseIds = declareView.HouseIds.Split(',');
        //                ZS_DeclareInfoService.UpdateDeclareHouse(declareView.Id, houseIds);
        //            }

        //            ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, declareView.Id, "编辑申报表信息", LoginUser.UName, LoginUser.Id);
        //            res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
        //        }
        //    }

        //    return res;
        //} 
        #endregion
        public string Edit(string viewStr)
        {
            string res = string.Empty;
            try
            {
                ViewDeclare declareView = JsonHandler.Deserialize<ViewDeclare>(viewStr);
                ZS_DeclareInfo editDeclare = ZS_DeclareInfoService.GetModel(declareView.Id);
                if (editDeclare == null)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
                }
                else
                {
                    editDeclare.BuildingId = declareView.BuildingId;
                    editDeclare.FamilyId = declareView.FamilyId;
                    editDeclare.MemberId = declareView.MemberId;
                    editDeclare.MemberName = declareView.MemberName;

                    bool success = ZS_DeclareInfoService.Update(editDeclare);
                    if (success)
                    {
                        if (!string.IsNullOrEmpty(declareView.MemberIds))
                        {
                            string[] memberIds = declareView.MemberIds.Split(',');
                            ZS_DeclareInfoService.AddDeclareJointApplicant(editDeclare.Id, memberIds);
                        }
                        if (declareView.HouseList.Count > 0)
                        {
                            ZS_DeclareInfoService.AddDeclareHouse(editDeclare.Id, declareView.BuildingId ?? 0, declareView.HouseList);
                        }

                        ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, editDeclare.Id, "编辑申报表信息", LoginUser.UName, LoginUser.Id);
                        res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                    }
                }
            }
            catch(Exception ex)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑失败!", ex.Message));
            }

            return res;
        }

        #endregion
        #region 删除
        public string Delete(int id)
        {
            ZS_DeclareInfo declareInfo = ZS_DeclareInfoService.GetModel(id);
            string res = string.Empty;
            if (declareInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                declareInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ZS_DeclareInfoService.Update(declareInfo);
                if (success)
                {
                    ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, declareInfo.Id, "删除申报表信息", LoginUser.UName, LoginUser.Id);
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion
        #region 申报的家庭，当事人，楼栋，房号，数据转核查

        public string TransferCheck()
        {
            JsonMessage msg = new JsonMessage();
            //从配置文件读取值 是否已经转核查 已转则不再转 否则可以转
            var _firstConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("FirstConfig") as Heima.OA.Common.MeConfiguration.FirstConfig;
            string isTransfer = _firstConfig.IsDeclareTransferCheck;
            if (!DataConvert.GetBool(isTransfer))
            {
                ZS_DeclareInfoService.DeclareTransferCheck();
                _firstConfig.IsDeclareTransferCheck = "true";
                _firstConfig.CurrentConfiguration.Save();
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "申报转核查成功!");
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "申报已经转核查，不能重复转!");
            }
            
            return JsonHandler.ToJson(msg);
        }

        #endregion
        #region 通过楼栋id获取楼栋基本信息、申报表个数、申报权利人数、已申报面积、该楼栋下的所有申报房屋的情况
        public string StatisticBuildingDeclareInfo(int Id)
        {
            JsonMessage msg = new JsonMessage();
            object obj = ZS_DeclareInfoService.BuildingDeclareInfo(Id);
            if(obj != null)
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(obj));
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "获取错误!");
            }
            return JsonHandler.ToJson(msg);
        }
        #endregion

    }
    [Serializable]
    public class ViewDeclare:ZS_DeclareInfo
    {
        /// <summary>
        /// 共同申报人ids
        /// </summary>
        public string MemberIds { get; set; }
        /// <summary>
        /// 房号ids
        /// </summary>
        //public string HouseIds { get; set; }
        public List<ZS_HouseInfo> HouseList { get; set; }

    }

}